﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyBlog2.Models;
using MyBlog2.Repository;

namespace MyBlog2.Controllers
{
    public class CommentController : Controller
    {
        private readonly ICommentRepository _commentRepository;
        private readonly IUserRepository _userRepository;

        public CommentController(ICommentRepository commentRepository,IUserRepository userRepository)
        {
            this._commentRepository = commentRepository;
            this._userRepository = userRepository;
        }


        /// <summary>
        /// 提交评论
        /// </summary>
        /// <param name="comment"></param>
        /// <returns></returns>
        [HttpPost]
        public void CommentPost(Comment comment)
        {
            if (comment.ArticleId != 0 && comment.Contents != null && comment.Nickname != null && comment.Email != null)
            {
                if (HttpContext.User.Identity.IsAuthenticated)  //管理员已登录
                {
                    var usr = _userRepository.GetUserInfo();
                    if (comment.Nickname == usr.Nickname && comment.Email == usr.Email)
                    {
                        comment.AdminComment = true;
                    }
                }
                _commentRepository.AddComment(comment);
                Redirect("/Home/Detial?id=" + comment.ArticleId);
            }
        }
    }
}